﻿using System.Collections.Generic;
using System;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace RHFrameWork
{
    public class DataObject
    {
        public class KeyValue
        {
            public string Key = "";
            public string Value = "";
        }



        /// <summary>
        /// 可序列化类，用来保存文件地址和二进制文件
        /// </summary>
        [Serializable]
        public class SerializeFileInfo
        {
            public SerializeFileInfo(string name, byte[] buffer)
            {
                fileName = name;
                fileBuffer = buffer;
            }
            private string fileName = null;

            public string FileName
            {
                get { return fileName; }
            }

            private byte[] fileBuffer = null;

            public byte[] FileBuffer
            {
                get { return fileBuffer; }
            }
        }

        private Dictionary<string, string> _Item = new Dictionary<string, string>();

        public Dictionary<string, string> Item
        {
            get { return _Item; }
            set { _Item = value; }
        }

        public void clear()
        {
            _Item.Clear();
        }

        /// <summary>
        /// 将字典转化为XML
        /// </summary>
        /// <returns>XML字符串</returns>
        public string ToXML()
        {
            XmlSerializer xs = new XmlSerializer(typeof(List<KeyValue>));
            List<KeyValue> lst = new List<KeyValue>();
            foreach (string key in _Item.Keys)
            {
                lst.Add(new KeyValue() { Key = key, Value = _Item[key] });
            }

            MemoryStream ms = new MemoryStream();
            xs.Serialize(ms, lst);
            string strRst = System.Text.Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            return strRst;
        }
        /// <summary>
        /// 读取XML文件，将相关内容加入字典中
        /// </summary>
        /// <param name="strXML">XML字符串</param>
        public void Load(string strXML)
        {
            this.clear();

            if (strXML.Trim() == "")
            {
                return;
            }
            XmlSerializer xs = new XmlSerializer(typeof(List<KeyValue>));

            //string str = System.Text.Encoding.UTF8.GetString(System.Text.Encoding.UTF8.GetBytes(strXML));

            MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(strXML));

            XmlTextReader reader = new XmlTextReader(ms);
            reader.Normalization = false;


            List<KeyValue> lst = (List<KeyValue>)xs.Deserialize(reader);

            _Item.Clear();

            foreach (KeyValue kv in lst)
            {
                _Item.Add(kv.Key, kv.Value);
            }
        }
        /// <summary>
        /// 设置主键 和 值
        /// </summary>
        /// <param name="Key"> 主键</param>
        /// <param name="Value">值</param>
        public void Set(string Key, string Value)
        {
            if (_Item.ContainsKey(Key) == true)
            {
                _Item[Key] = Value;
            }
            else
            {
                _Item.Add(Key, Value);
            }
        }
        /// <summary>
        /// 通过主键 获取 值
        /// </summary>
        /// <param name="Key">主键</param>
        /// <returns>返回string 值</returns>
        public string Get(string Key)
        {
            if (_Item.ContainsKey(Key) == true)
            {
                return _Item[Key];
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 返回参数的个数
        /// </summary>
        public int Count
        {
            get
            {
                return _Item.Count;
            }
        }

    }
}
